import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", redirect: '/home' },
    { path: "/home", name: "home", components:{
       default: ()=>import("@/views/Home/HomeView.vue"),
       footer:()=>import("@/components/FooterCom.vue"),
    } },
    { path: "/kind", name: "kind", components:{
      default:()=>import("@/views/Kind/KindView.vue"),
      footer:()=>import("@/components/FooterCom.vue")
    } },
    { path: "/cart", meta: { requiresAuth: true }, name: "cart", components:{
      default:()=>import("@/views/Cart/CartView.vue"),
       footer:()=>import("@/components/FooterCom.vue")
    } },
    { path: "/my", meta: { requiresAuth: true }, name: "my", components:{
      default:()=>import("@/views/My/MyView.vue"),
      footer:()=>import("@/components/FooterCom.vue")
    } },
    { path: "/detail/:proid", meta: { requiresAuth: true }, name: "detail", component: () => import("@/views/Detail/DetailView.vue") },
    { path: "/register", name: "register", component: () => import("@/views/Register/RegisterView.vue") ,children:[
      {path:"/register/",redirect:"/register/tel"},
      {path:"tel",component:()=>import("@/views/Register/components/CheckTel.vue")},
      {path:"msg",component:()=>import("@/views/Register/components/SendMsg.vue")},
      {path:"pass",component:()=>import("@/views/Register/components/SetPass.vue")}
    ]},
    {
      path:"/login",name:"login",component:()=>import("@/views/Login/LoginView.vue")
    },
    {
      path:"/set",name:"set",component:()=>import("@/views/My/SetView.vue")
    }
  ],
})
import auth from '@/utils/auth'
// 路由守卫
router.beforeEach((to, from) => {
  // 如果你访问是 需要权限认证的路由并且没有登录
  if (to.meta.requiresAuth && !auth.isLoggedIn()) {
    // 此路由需要授权，请检查是否已登录
    // 如果没有，则重定向到登录页面
    return {
      path: '/login',
      // 保存我们所在的位置，以便以后再来
      query: { redirect: to.fullPath },
    }
  }
})

export default router
